import {
  Controller,
  Post,
  Body,
  Req,
  Get,
  Query,
  UseGuards,
  Put,
  UseInterceptors,
  UploadedFile,
} from '@nestjs/common';
import { UserService } from './user.service';
import {
  LoginDto,
  RedactDto,
  RegisterDto,
  RemoveDto,
  SendVerifyCodeDto,
} from './dto/user.dto';
import { Public } from '../../common/decorators/public.decorator';
import { ACCOUNT_ID } from '../../common/constants/auth.constant';
import {
  PageQueryParam,
  PaginationPipe,
} from '../../common/pipes/pagination.pipe';
import { Request } from 'express';
import { Throttle, ThrottlerGuard } from '@nestjs/throttler';
import { RequirePermission } from '../../common/decorators/require-permission.decorator';
import { FileInterceptor } from '@nestjs/platform-express';

@UseGuards(ThrottlerGuard)
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  /**
   * 发送验证码
   * @param body
   * @returns
   */
  @Throttle({ default: { limit: 3, ttl: 60000 } })
  @Public()
  @Post('send-verify-code')
  async sendVerifyCode(@Body() body: SendVerifyCodeDto) {
    await this.userService.sendVerifyCode(body);
    return null;
  }

  /**
   * 注册
   * @param body
   * @returns
   */
  @Public()
  @Post('register')
  async register(@Body() body: RegisterDto) {
    await this.userService.register(body);
    return null;
  }

  /**
   * 登录
   * @param body
   * @returns
   */
  @Public()
  @Post('login')
  async login(@Body() body: LoginDto, @Req() request: Request) {
    return await this.userService.login(body, request);
  }

  /**
   * 编辑用户信息
   * @param body
   * @param request
   * @returns
   */
  @Put('redact')
  async redact(@Body() body: RedactDto, @Req() request: Request) {
    return await this.userService.redactUserInfo(request[ACCOUNT_ID], body);
  }

  /**
   * 确实密码删除账号
   * @param body
   * @param request
   * @returns
   */
  @Post('remove')
  async remove(@Body() body: RemoveDto, @Req() request: Request) {
    return await this.userService.removeByPassword(
      request[ACCOUNT_ID],
      body.password,
    );
  }

  /**
   * 分页查询用户信息
   * @param body
   * @param request
   * @returns
   */
  @RequirePermission('sys:user:find')
  @Post('find-page')
  async findPage(@Body(PaginationPipe) body: PageQueryParam) {
    return await this.userService.findAndCount(body);
  }

  /**
   * 分页查询用户信息
   * @param body
   * @param request
   * @returns
   */
  @Get('find-page')
  async getFindPage(@Query(PaginationPipe) param: PageQueryParam) {
    return await this.userService.findAndCount({
      ...param,
      withDeleted: true,
    });
  }

  /**
   * 上传并修改用户账号头像
   * @param file 头像文件
   * @param request
   * @returns
   */
  @UseInterceptors(FileInterceptor('file'))
  @Put('update-avatar')
  async updateHeadPortrait(
    @UploadedFile() file: Express.Multer.File,
    @Req() request: Request,
  ) {
    let result = null;
    if (file) {
      result = await this.userService.uploadAndUpdateAvatar(
        file.originalname,
        file.buffer,
        request[ACCOUNT_ID],
      );
    }
    return result;
  }

  @Get('gender-options')
  async getUserGenderOptions() {
    return await this.userService.findUserGenderOptions();
  }
}
